package com.hspedu.enum_;

/**
 * @Author Stringzhua
 * @Date 2024/3/6 21:27
 * description:演示Enum类各种方法的使用
 * <p>
 * SPRING("春天", "温暖"), WINTER("冬天", "寒冷"), AUTUMN("秋天", "凉爽"),
 * SUMMER("夏天", "炎热"), What();
 */
public class EnumMethod {
    public static void main(String[] args) {
        //以使用Season02枚举类,来演示各种
        Season02 autumn = Season02.AUTUMN;
        //1.输出枚举对象的名称
        System.out.println(autumn.name());//AUTUMN
        //2.ordinal()输出的是该枚举对象的第几个/编号 从0开始编号
        //autumn枚举对象是第三个,因此输出2
        System.out.println(autumn.ordinal());//2
        System.out.println("=============================");
        //3.从反编译可以看到values方法,返回的是Season02[]
        //含有定义的所有枚举对象
        Season02[] values = Season02.values();
        for (Season02 season02 : values) {//增强for循环 遍历取出枚举对象
            System.out.println("season02 = " + season02);
        }

//        int[] nums = {1, 2, 9};
//        //普通的for循环
//        System.out.println("======普通的for=========");
//        for (int i = 0; i < nums.length; i++) {
//            System.out.println(nums[i]);
//        }
//        System.out.println("======增强的for=========");
//        //增强for循环
//        for (int i : nums) {//依次从nums数组中取出元素,赋给i.如果取出完毕则退出for循环
//            System.out.println("i = " + i);
//        }

        //4.将字符串转换成枚举对象,要求从字符串必须为已有的常量名,否则报异常!
        //step1.根据输入的名称,到Season02的枚举对象去查找
        //step2.如果找到了,就返回.如果没有找到,就报错.
        Season02 autumn1 = Season02.valueOf("AUTUMN");
        System.out.println(autumn1);
        System.out.println(autumn == autumn1);//true
        //5.compareTo,比较两个枚举常量,比较的就是位置号
        //1.用Season02.AUTUMN枚举对象的编号和Season02.SUMMER枚举对象的编号进行比较
        //2.返回-1
        /*
        *  public final int compareTo(E o) {
            Enum<?> other = (Enum<?>)o;
            Enum<E> self = this;
                if (self.getClass() != other.getClass() && // optimization
                    self.getDeclaringClass() != other.getDeclaringClass())
                    throw new ClassCastException();
                return self.ordinal - other.ordinal;
        }
        * Season02.AUTUMN的编号[2]-Season02.SUMMER的编号[3]
        *
        * */
        System.out.println(Season02.AUTUMN.compareTo(Season02.SUMMER));//-1
        System.out.println(Season02.SUMMER.compareTo(Season02.AUTUMN));//1
    }
}

